package org.mine.design.flyweight.service.flyweight;

import org.mine.design.flyweight.enums.DemoEnum;
import org.mine.design.flyweight.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class DemoFlyweightFactory {

    @Autowired
    private List<DemoService> demoServices;

    private static final Map<DemoEnum, DemoService> MAP = new HashMap<>();

    @PostConstruct
    private void init() {
        demoServices.forEach(
                service -> {
                    MAP.put(service.getDemoEnum(), service);
                }
        );
    }

    public static DemoService getDemoServiceByDemoEnum(DemoEnum demoEnum) {
        return MAP.get(demoEnum);
    }

}
